<%@page import="java.util.Iterator"%>
<%@page import="user.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="user.UserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*,common.DBUtil"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery-3.2.1.js"></script>
<title>用户管理中心</title>
<style type="text/css">
body {
	background-image: url(../images/1.png);
}
</style>
<script type="text/javascript">
	window.onload = function color() {//窗口加载时调用  
		var table = document.getElementById("table");//根据table的 id 属性值获得对象  
		var rows = table.getElementsByTagName("tr");//获取table类型的tr元素的列表  
		for (var i = 0; i < rows.length; i++) {
			if (i % 2 == 0) {
				rows[i].style.backgroundColor = "red";//偶数行时背景色为#EAF2D3  
			} else {
				rows[i].style.backgroundColor = "green";//单数行时背景色为white  
			}
		}
	}
</script>
<style>
a {
	text-decoration: none
}
</style>
<script type="text/javascript">
	function reallyDel(uuu) {
		var hint = "确认要删除用户";
		var jg = confirm(hint);
		if (jg) {
			return true;
		}
		return false;
	}
</script>
<script type="text/javascript">
	function reallyRes(uuu) {
		var hint = "确认重置密码";
		var jg = confirm(hint);
		if (jg) {
			return true;
		}
		return false;
	}
</script>
<script type="text/javascript">
	function reallyPow(uuu) {
		var hint = "确认修改权限";
		var jg = confirm(hint);
		if (jg) {
			return true;
		}
		return false;
	}
</script>
<script type="text/javascript">
	function reallySta(uuu) {
		var hint = "确认修改状态";
		var jg = confirm(hint);
		if (jg) {
			return true;
		}
		return false;
	}
</script>
<script type="text/javascript">
 //批量删除
	function delall() {
		alert("确定删除吗？？");
		var chekId = [];//先定义一个空的数组	
		var chek=new String();
		if ($("input[type='checkbox']:checked").length > 0) {
			$("input[type='checkbox']:checked").each(function(i) {
				chekId[i] = $(this).val();//选中的框的值赋值给定义的数组
			})
			//把数组转换为字符串
			for(var i = 0;i<chekId.length;i++){
				chek = chek+chekId[i]+",";
			}
			//赋值完之后再传到后台
			window.location.href = "${pageContext.request.contextPath}/DeleteSomeServlet?ckekId="
					+ chek;
		} else {
			alert("请选择你要删除的信息");
		}
	}
 //全选
//全选
 function qx(){
	var check1 = document.getElementsByName("check");
	for(var x =0;x <check1.length;x++){
		check1[x].checked=true;
		}
	
}
//反选
function fx(){
	var ck = document.getElementsByName("check");
	for(var x =0;x < ck.length;x++){
		if(ck[x].checked){
			ck[x].checked=false;
		}else{
			ck[x].checked=true;
		}
	}
}
</script>
</head>
<body>
	<h2>新闻发布系统>>用户管理</h2>
	<form align="center" action="/Lesson_310/selectUser" method="post"
		name="frm_register">
		请你输入你要查询的用户名：<input type="text" name="uname" align="center"> <input
			type="submit" name="btn" value="查询">
	</form>

	<%
		if (session.getAttribute("list") == null) {
			return;
		}
	%>
	<table id="table" border="1" align="center" width="80%" cellspacing="1" cellpading="1">
		<tr>
			<td>选择</td>
			<td>编号</td>
			<td>用户名</td>
			<td>性别</td>
			<td>职业</td>
			<td>爱好</td>
			<td>类型</td>
			<td>状态</td>
			<td>重置密码</td>
			<td>修改权限</td>
			<td>更改状态</td>
			<td>删除用户</td>
			<td>修改用户</td>
			<td>查看用户</td>
			<%
				int pages = 0; //待显示页面
				int count = 0; //总条数
				int totalpages = 0; //总页数
				int limit = 5; //每页显示记录条数    
				//计算记录总数的第二种办法：使用mysql的聚集函数count(*)
				DBUtil db = new DBUtil();
				ResultSet sqlRst = db.executeQuery("select count(*) from user");
				if (sqlRst.next()) {
					count = sqlRst.getInt(1);//结果为count(*)表，只有一列。这里通过列的下标索引（1）来获取值
				}
				//由记录总数除以每页记录数得出总页数
				totalpages = (int) Math.ceil(count / (limit * 1.0));
				//获取跳页时传进来的当前页面参数
				String strPage = request.getParameter("pages");
				//判断当前页面参数的合法性并处理非法页号（为空则显示第一页，小于0则显示第一页，大于总页数则显示最后一页）
				if (strPage == null) {//如何strpage为空则pages为1
					pages = 1;
				} else {
					try {
						pages = java.lang.Integer.parseInt(strPage);
					} catch (Exception e) {
						pages = 1;
					}

					if (pages < 1) {//如果pages小于1则pages=1
						pages = 1;
					}

					if (pages > totalpages) {//如果pages大于totalpages则pages等于totalpages
						pages = totalpages;
					}
				}
				//由(pages-1)*limit算出当前页面第一条记录，由limit查询limit条记录。则得出当前页面的记录
				UserDao userDao = new UserDao();
				ArrayList<User> list = userDao.doUserPage(pages, limit);
				Iterator<User> iterator = list.iterator();
				while (iterator.hasNext()) {
					User user = iterator.next();
					int uid = user.getUid();
					String sex = user.getSex();
					if (sex.equals("1")) {
						sex = "女";
					} else {
						sex = "男";
					}
					int type = user.getType();
					String type_s;
					if (type == 0) {
						type_s = "普通用户";
					} else {
						type_s = "管理员";
					}
					int isValid = user.getIsValid();
					String isValid_s;
					if (isValid == 0) {
						isValid_s = "无效";
					} else {
						isValid_s = "有效";
					}
			%>
		
		<tr>
			<td>
			    <input type="checkbox" name="check" value="<%=uid%>">
			</td>
			<td><%=uid%></td>
			<td><%=user.getUname()%></td>
			<td><%=sex%></td>
			<td><%=user.getProfession()%></td>
			<td><%=user.getFavourite()%></td>
			<td><%=type_s%></td>
			<td><%=isValid_s%></td>
			<td><a href="../User_pass_reset?uid=<%=uid%>"
				onclick="return reallyRes()">重置密码</a></td>
			<td><a href="../User_power_update?uid=<%=uid%>"
				onclick="return reallyPow()">修改权限</a></td>
			<td><a href="../User_stata_update?uid=<%=uid%>"
				onclick="return reallySta()">更改状态</a></td>
			<td><a href="../User_deleteServlet?uid=<%=uid%>"
				onclick="return reallyDel()">删除</a></td>
			<td><a
				href="./userModify.jsp?uid=<%=uid%>&uname=<%=user.getUname()%>&note=<%=user.getNote()%>"
				onclick="return reallyUPD()">修改用户</a></td>
			<td><a
				href="../User_select_pesson?uid=<%=uid%>&uname=<%=user.getUname()%>">查看用户</a></td>
		</tr>
		<%
			}
		%>
		<form name="f1" method="POST" action="user_select.jsp" onSubmit="return checknum()">
			<table border="0" align="center">
				<tr>
				    <td><input type="button" value="全选" onclick="qx()" id="check1"></td>
				    <td><input type="button" value="反选" onclick="fx()" id="check"></td>
					<td><input type="button" value="批量删除" onclick="delall()"></td>
					<td>第<%=pages%>页 共<%=totalpages%>页 <a
						href="user_select.jsp?pages=1">首页</a></td>
					<td><a
						href="user_select.jsp?pages=<%=(pages < 1) ? pages : (pages - 1)%>">
							上一页</a></td>
					<td><a
						href="user_select.jsp?pages=<%=(pages >= totalpages) ? totalpages : (pages + 1)%>">
							下一页</a></td>
					<td><a href="user_select.jsp?pages=<%=totalpages%>">最后一页</a></td>
					<td>转到第:<input type="text" name="page" size="8">页<input
						type="submit" value="GO" name="cndok"></td>
				</tr>
			</table>
			
		</form>
</body>
</html>